package com.mbuswidget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.sax.StartElementListener;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

public class MBusWidgetProvider extends AppWidgetProvider {

	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		// TODO Auto-generated method stub
		// super.onDeleted(context, appWidgetIds);

		Intent intent = new Intent(context, MBusWidgetService.class);
		intent.putExtra(MBusWidgetConstants.WIDGETS_DELETED, appWidgetIds);
		context.startService(intent);

//		Toast.makeText(context, "onDeleted()", Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onDisabled(Context context) {
		// TODO Auto-generated method stub
		// super.onDisabled(context);
//		Toast.makeText(context, "onDisabled()", Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onEnabled(Context context) {
		// TODO Auto-generated method stub
		// super.onEnabled(context);

//		Toast.makeText(context, "onEnabled()", Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		final int N = appWidgetIds.length;

//		Toast.makeText(context, "onUpdate()", Toast.LENGTH_SHORT).show();
		//Log.w("mbuswidget", "onUpdate called for mbuswidget " + N);

		// Perform this loop procedure for each App Widget that belongs to this
		// provider
		for (int i = 0; i < N; i++) {
			int appWidgetId = appWidgetIds[i];

			// // Create an Intent to launch ExampleActivity
			Intent intent = new Intent(context, MBusWidgetService.class);
			intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
//			Toast.makeText(context, "Widgets " + appWidgetIds.length,
//					Toast.LENGTH_SHORT).show();

			
			SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

			RemoteViews views;
			if (MBusWidgetConstants
			.GetWidgetStopList(prefs, appWidgetId).size() == 0) {
				views = new RemoteViews(context.getPackageName(),
						R.layout.mbuswidgetinitialized);
			} else {
				views = new RemoteViews(context.getPackageName(),
						R.layout.mbuswidget);
				
				// TODO: Insert code that notifies the user that the service is waiting for data...
			}

			// To react to a click we have to use a pending intent as the
			// onClickListener is
			// excecuted by the homescreen application
			PendingIntent pendingIntent = PendingIntent.getService(
					context.getApplicationContext(), 0, intent,
					PendingIntent.FLAG_UPDATE_CURRENT);
			views.setOnClickPendingIntent(R.id.reloadBtn, pendingIntent);

			// Make entire widget clickable

			Intent openConfigIntent = new Intent(context,
					MBusWidgetConfiguration.class);
			openConfigIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
					appWidgetId);
			openConfigIntent.setAction(MBusWidgetConstants.PREFS_NAME + System.currentTimeMillis());
			PendingIntent pendingOpenConfig = PendingIntent.getActivity(
					context, 0, openConfigIntent, 0);
			views.setOnClickPendingIntent(R.id.mbusWidgetRelativeLayout,
					pendingOpenConfig);

			//Log.w("mbuswidget", "Update widget " + appWidgetId);

			// views.setOnClickPendingIntent(R.id.button1, pendingIntent);

			// Tell the AppWidgetManager to perform an update on the current app
			// widget
			appWidgetManager.updateAppWidget(appWidgetId, views);

			context.startService(intent);
		}
	}
}
